#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/cc/add_module.py
# Compiled at: 2017-11-16 15:44:28
"""
Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
import json
from django import forms
from common.forms import BaseComponentForm, ListField, TypeCheckField
from common.constants import API_TYPE_OP
from components.component import Component
from .toolkit import tools, configs

class AddModule(Component):
    """
    apiLabel 新建模块
    apiMethod POST
    
    ### 功能描述
    
    新建模块
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段      |  类型      | 必选   |  描述      |
    |-----------|------------|--------|------------|
    | app_id    |  int       | 是     | 业务ID |
    | set_id    |  int       | 是     | 集群ID |
    | module_name |  string  | 否     | 模块名，多个用英文逗号分隔 |
    | operator  |  string    | 否     | 操作人 |
    | bak_operator | string  | 否     | 备份操作人 |
    | module_type | int      | 否     | 模块类型，1: 普通, 2: 数据库 |
    | properties | dict      | 否     | 模块属性，自定义属性用customerxx来修改 |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "app_id": 1,
        "set_id": 10,
        "module_name": "test1,test2",
        "module_type": 1,
        "operator": "user1",
        "bak_operator": "user2",
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "message": "",
        "data": {},
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP
    host = configs.host

    class Form(BaseComponentForm):
        app_id = forms.IntegerField(label=u'业务ID', required=True)
        set_id = forms.IntegerField(label=u'集群ID', required=True)
        module_name = ListField(label=u'模块名', required=False)
        operator = forms.CharField(label=u'操作人', required=False)
        bak_operator = forms.CharField(label=u'备份操作人', required=False)
        module_type = forms.IntegerField(label=u'模块类型', required=False)
        properties = TypeCheckField(label=u'模块属性', promise_type=dict, required=False)

        def clean(self):
            data = self.cleaned_data
            return {'ApplicationID': data['app_id'],
               'SetID': data['set_id'],
               'ModuleName': (',').join(data['module_name']),
               'Operator': data['operator'],
               'BakOperator': data['bak_operator'],
               'ModuleType': data['module_type'],
               'properties': json.dumps(data['properties'])
               }

    def handle(self):
        client = tools.CCClient(self.outgoing.http_client)
        self.response.payload = client.post_request(self.host, '/api/module/addModule', data=self.form_data)
